//NEW_SUDUKU.ino
#include <Enerlib.h>

Energy energy;
//display driver define
#define KEYTIME    						300
#define	RAMSIZE							16
#define	CMD_AIP1629_WRITE				0x40
#define	CMD_AIP1629_READ					0x42
#define	CMD_AIP1629_ADDRMODE				0x44
#define	CMD_AIP1629_STARTADDR(addr)		addr|0xC0
#define POINTSIGN                       0x80
#define BACKGROUND_SIGN					0x00//40
#define HDIGN                   		0x76

//hardware define
#define OFFLED 				A3				//LDDS 开关
#define	AIP1629_DIN   		3			//AIP1629数据输入
#define	AIP1629_DIO   		A5				//AIP1629数据输出
#define	AIP1629_CLK   		4				//AIP1629时钟输入
#define	AIP1629_STB   		5				//AIP1629片选
#define AUDIO				A4	  
#define	LED1   				A0			//LED
#define	LED2   				A1			//LED
#define	LED3   				6			//LED
#define	LED4   				7			//LED
#define	LED5   				8			//LED
#define	LED6   				9			//LED
#define	LED7   				10			//LED
#define	LED8   				11			//LED
#define	LED9   				12			//LED			
#define	LEDA   				13			//LED
#define MULTIFUNCTION 		2
#define BATTERYLEVEL        A6
#define HADRUSBIN			A7
#define CHARGERSTATE		A2
//audio information
#define NOWPOWEROFF             0x01
#define NOWPOWERON              0x00
#define READY2NEXT				0x02
#define USBPLUGIN				0x03
#define FULLYCHARGED            0x04
#define LOWBATTERY				0x05
#define SWITCHTOPIC             0x06
#define EARSEALL				0x07
#define WEAKINGUP				0x08
#define SLEEPING 				0x09

//function define
#define nop() 					delayMicroseconds(2);//(5);
#define	AIP1629_STB_HIGH()	    digitalWrite(AIP1629_STB, 1)			        
#define	AIP1629_STB_LOW()		digitalWrite(AIP1629_STB, 0)			         

#define	AIP1629_CLK_HIGH()		digitalWrite(AIP1629_CLK, 1)	
#define	AIP1629_CLK_LOW()		digitalWrite(AIP1629_CLK, 0)		         

#define	AIP1629_DIO_HIGH()		digitalWrite(AIP1629_DIO, 1)	        
#define	AIP1629_DIO_LOW()		digitalWrite(AIP1629_DIO, 0)
boolean runOnce = true;
char stateBTN = 0;
char stateCHR = 0;
char lastKey1 = 0,lastKey2 = 0;
char offAll = 1;

unsigned char lastKeyBuf0,lastKeyBuf1,lastKeyBuf2,lastKeyBuf3;
// unsigned char add = 0;
unsigned int k;
unsigned char count;
long lastTime = 0;
long autopower = 0;
long lastTime_mouse =0;
unsigned long sudukuTimeBegin = 0,sudukuTimeStop = 0;
unsigned char suduNum = 0;
unsigned int state = 0;
unsigned char backGroud[16] = {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
unsigned char mosueDeadArray[16] = {0x08,0x08,0x08,0x08,0x08,0x08,0x08,0x08,0x08,0x08,0x08,0x08,0x08,0x08,0x08,0x08};
unsigned char LED[16] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0};	//LED[0]-LED[15]对应16个数码管,TM1638最多只能接10个()，后6个无效，初始化显示成0
unsigned char Buf1[16] = {0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff};	//显示缓存1，存放共阴显示数据
unsigned char Buf2[16] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0};	//显示缓存2，存放共阳显示数据
unsigned char KeyBuf[4] = {123,0,0,0};
unsigned char PCODE[5] = {0x86,0xdb,0xcf,0xe6,0x00};//,0xed,0xfd,0x87,0xef,0xdf,0xf0};
unsigned char CODE[5] = {0x06,0x5b,0x4f,0x66,0x06};//,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x80};//数码管显示0-9的代码(共阴接法） ，0x00是数码管关闭的代码                                                                
unsigned char DspBuf[16] = {0x00,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0};//16字节显示RAM
unsigned char currentProblem[16]={0,3,0,0,0,0,1,3,2,2,1,1,1,0,0,3};
unsigned char counter16Bit[16]={0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0};
unsigned char suffer[16]={};
unsigned char finalResult[16]={};
unsigned char segConvered[16] = {};
unsigned char in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0,0},
	{0,1,3,4,0,0,0,0,0,3,1,0,0,0,0,0},
	{0,3,0,0,0,0,3,0,4,0,0,0,0,0,0,1},
	{0,2,1,0,0,0,0,0,0,0,4,0,0,3,0,0},
	{0,2,4,0,0,0,0,0,0,1,0,0,3,0,2,0},
	{0,3,0,0,0,0,0,3,1,0,0,0,0,0,4,0},
	{0,0,2,0,0,0,0,3,1,0,0,0,0,0,0,4},
	{4,0,0,0,0,0,3,0,0,4,0,0,0,0,0,2},
	{0,0,0,4,2,0,0,0,0,1,0,0,0,0,1,0}
};

//  class Grid
// {
//   public:
//    unsigned char x;
//    unsigned char y;
//    unsigned long minTime;
//    boolean over = false;
// };

//  Grid::Grid(int n,int s)
// {
//   num=n;
//   score=s;
//  }
//  void Grid::Print()
// {
//   //cout<<num<<"\t"<<score<<endl;
//  } 

// unsigned char column = 4,row = 4;
// unsigned char mineSweeper[4][4] = {};
// void CreateMap()// 创建初始化数组
// {
// 	for(int i=0; i<row; i++){
// 	    for(int j=0; j<column; j++){
// 	      mineSweeper[i][j] = 0;  
// 	    }
// 	}
// }
// void ComputerNumber() // 遍历数组，查找每个格子周围雷的个数
// {

// }
// int CountBoom(int x, int y)  // 计算每个方格的周围雷数（九宫格形状，除去中心位置的）
// {
    
// }
// void Open(int x, int y)  // 打开雷数为0的格子
// {

// }
// void boomadd(int boomCount)  // 为地图中添加的雷数
// {

// }
void char2HexSeg(unsigned char inchar[],unsigned char segType[])
{
	unsigned char seg[16]={};
	for(int i=0; i<16; i++)
	{
		switch (inchar[i]) 
		{
			case 1:
			seg[i]=segType[0];
			break;
			case 2:
			seg[i]=segType[1];
			break;
			case 3:
			seg[i]=segType[2];
			break;
			case 4:
			seg[i]=segType[3];
			break;
			default:
	          ;// do something;   
	      }
	    // Serial.print("0x");
	    // Serial.print(segConvered[i],HEX);
	    // Serial.print(",");
	}
	// Serial.println();
	//return seg[16];
	segConvered[0] = seg[0];
	segConvered[1] = seg[2];
	segConvered[2] = seg[4];
	segConvered[3] = seg[6];
	segConvered[4] = seg[8];
	segConvered[5] = seg[10];
	segConvered[6] = seg[12];
	segConvered[7] = seg[14];
	segConvered[8] = seg[1];
	segConvered[9] = seg[3];
	segConvered[10] = seg[5];
	segConvered[11] = seg[7];
	segConvered[12] = seg[9];
	segConvered[13] = seg[11];
	segConvered[14] = seg[13];
	segConvered[15] = seg[15];

}
void showArray16(unsigned char indata[],int type)
{
	for(int i=0; i<16; i++){
		if(type == HEX)
		{
		    Serial.print("0x");
		}
		else
		Serial.print(indata[i],type);
		Serial.print(",");
	}
	Serial.println();
}
void row2Column(unsigned char segOK[])
{
	inPut[0] = segOK[0];
	inPut[4] = segOK[1];
	inPut[8] = segOK[2];
	inPut[12] = segOK[3];
	inPut[1] = segOK[4];
	inPut[5] = segOK[5];
	inPut[9] = segOK[6];
	inPut[13] = segOK[7];
	inPut[2] = segOK[8];
	inPut[6] = segOK[9];
	inPut[10] = segOK[10];
	inPut[14] = segOK[11];
	inPut[3] = segOK[12];
	inPut[7] = segOK[13];
	inPut[11] = segOK[14];
	inPut[15] = segOK[15];
	// showArray16(inPut,1);
}


/* MCU一次输出8个bit */
void	AIP1629_OutByte(unsigned char dat)
{
	unsigned char i;

	for(i=0;i<8;i++)
	{
		AIP1629_CLK_LOW();		 
		if((dat & 0x01) == 0x01)
			AIP1629_DIO_HIGH();
			else
			AIP1629_DIO_LOW();
			//nop();											//单片机频率高时应适延时 (脉宽大于400ns)
		AIP1629_CLK_HIGH();											//上升沿输数据,低位开始
		dat >>= 1;
	}
}		         					
/* MCU一次接收8个bit */
unsigned char	AIP1629_InByte(void)
{
	unsigned char i;
	unsigned char dat = 0;
	
	AIP1629_DIO_HIGH();												//如需要，必须在此设置I/O方向为输入
		for(i = 0; i < 8; i++)
		{		
			AIP1629_CLK_LOW();
		dat >>= 1;												//从低位开始读取
		if(digitalRead(AIP1629_DIN)) dat += 0x80;
			AIP1629_CLK_HIGH();
		}
		return(dat);
	}

/* 向AIP1629发送命令 */
void	AIP1629_SendCmd(unsigned char cmd)
{
	AIP1629_STB_HIGH();											//初始化AIP1629(高电平)
		AIP1629_CLK_HIGH();

	AIP1629_STB_LOW();											//片选(低电平)
	//nop();													//等待AIP1629串口初始化完成
	AIP1629_OutByte(cmd);
		AIP1629_STB_HIGH();	
	}
/* 向AIP1629发送地址 */
void	AIP1629_SendAddr(unsigned char addr)
{
	AIP1629_STB_HIGH();											//初始化AIP1629
		AIP1629_CLK_HIGH();

	AIP1629_STB_LOW();											//片选，低电平有效
	//nop();													//等待AIP1629串口初始化完成
	AIP1629_OutByte(addr);
}

/* 向指定显示寄存器写入1byte数据 */
void	AIP1629_WriteByte( unsigned char addr, unsigned char dat )  
{
	AIP1629_SendAddr( addr );
		AIP1629_OutByte( dat );
			AIP1629_STB_HIGH();
		}
/* 向指定起始显示寄存器写入数据块 */
void	AIP1629_WriteBlock( unsigned char addr, unsigned char len, unsigned char* buf )
{
	AIP1629_SendAddr( addr );
		while ( len -- ) AIP1629_OutByte( *buf++ );
			AIP1629_STB_HIGH();
		}
/* 连续读len个字节的按键数据,存放在buf指定的连续内存中 */
void	AIP1629_ReadKey( unsigned char len, unsigned char* buf )
{
	AIP1629_SendAddr(CMD_AIP1629_READ);//这条命令不能用AIP1629_SendCmd送
		while ( len -- ) *buf++ = AIP1629_InByte();
			AIP1629_STB_HIGH();
		}
/* 配置AIP1629 */
void Config(boolean state)
{
	delay(200);
	Serial.begin(115200);
	for(int i=6; i<14; i++)
    {
        pinMode(i,OUTPUT);
  	}
	pinMode(AUDIO, OUTPUT);
	pinMode(AIP1629_CLK, OUTPUT);
	pinMode(AIP1629_DIO, OUTPUT);
	pinMode(AIP1629_STB, OUTPUT);
	pinMode(CHARGERSTATE, INPUT_PULLUP);
	pinMode(AIP1629_DIN, INPUT);
	pinMode(OFFLED, OUTPUT);
	pinMode(LED2, OUTPUT);
	pinMode(LED1, OUTPUT);
	delay(200);
  
	if(state)
	{
		digitalWrite(OFFLED, 1);
		delay(200);
		AIP1629_CLK_HIGH();
		AIP1629_DIO_HIGH();
		AIP1629_STB_HIGH();	
		delay(200);
		AIP1629_SendCmd(0x03);								/*写AIP1629显示模式*/
		AIP1629_SendCmd(0x40);							    /*写数据到显示寄存器的方式采用地址自动加一*/
		//clear ram
		AIP1629_SendCmd(0x88);	
		Serial.println("ON ALL DEVICE");							//开显示，亮度可以通过改变低三位调节 
	}
	if(!state)
	{
	  AIP1629_CLK_LOW();
	  AIP1629_DIO_LOW();
	  AIP1629_STB_LOW();	  
	  digitalWrite(OFFLED, 0);
	  delay(200);
	  Serial.println("OFF ALL DEVICE");
	}
	Serial.println("Config OK!");
	
	
}
/* AIP1629 DEMO1 地址自动加一*/
void AIP1629_DEMO1(void)
{	
	//convert1(Buf1,Buf2,14);
	AIP1629_WriteBlock(CMD_AIP1629_STARTADDR(0),16,Buf1);  
	// AIP1629_SendCmd(0x8d);//开显示，亮度可以通过改变低三位调节	 config中
	AIP1629_ReadKey(4,KeyBuf); //读按键  
}

/* AIP1629 DEMO2 固定地址模式*/
void AIP1629_DEMO2(void)
{	
	unsigned char i;

	//convert1(Buf1,Buf2,14);
	for(i=0;i<16;i++)
	{
		delay(1000);
		AIP1629_WriteByte(0xc0+i,Buf1[i]);
	}  
	// AIP1629_SendCmd(0x8d);//开显示，亮度可以通过改变低三位调节
	//AIP1629_ReadKey(4,KeyBuf); //读按键  
}

void clearDisplay()
{
	// unsigned char temp[16];
	// char2HexSeg(currentProblem,PCODE);
	// AIP1629_WriteBlock(CMD_AIP1629_STARTADDR(0),16,segConvered);
}

// void setUpSuduku(uint8_t problemIndex,uint8_t level)
// {
// 	unsigned char tempIndex[16]={};
// 	for(int i=0; i<16; i++)
// 	{
// 	    tempIndex[i] = pgm_read_byte( & (currentProAaary[problemIndex][i]));//currentProAaary[problemIndex][i];
// 		Serial.print(",");
// 		Serial.print(pgm_read_byte( & (currentProAaary[problemIndex][i])));
// 	}
// 	Serial.println();
// 	char2HexSeg(tempIndex,PCODE);
// 	AIP1629_WriteBlock(CMD_AIP1629_STARTADDR(0),16,segConvered);
// }

void keySetSeg()
{

}
void showLed(char ledNum,boolean status)
{


	for(int i=13; i>=14-ledNum; i--)
	{

	 digitalWrite(i, status);  
	 
	}
	if(ledNum>=10)
	{
		digitalWrite(LED2, status);  
	}  
	if(ledNum>=9)
	{
		digitalWrite(A0, status);  
	}   
	
}
boolean checkWin(unsigned char answerIndex)
{
	unsigned char a = 0 ;
	boolean b = false;
	for(int i=0; i<16; i++)
	{
	   finalResult[i] = pgm_read_byte( & (currentAnsAaary[answerIndex][i])) ;
	}
	for(int i=0; i<16; i++)
	{

	    if(finalResult[i]==counter16Bit[i])
	    {
	       a++; 
	    }
	}

	if(a==16)
	{
	   b = true; 
	   sudukuTimeStop = millis();
	}
	// showArray16(finalResult,1);
	// showArray16(counter16Bit,1);
	// Serial.println(a);
	return b;
}

void playSuduku(unsigned char problemIndex)
{
	static unsigned int timeThreshold = 200;
	static unsigned char counterForKey = 0;
	if(runOnce == true)
	{
		digitalWrite(OFFLED, 1); 

		for(int i=0; i<16; i++)
		{
		    suffer[i] = pgm_read_byte( & (currentProAaary[problemIndex][i]));
		}
		memcpy(counter16Bit,suffer,16*sizeof(unsigned char));
		char2HexSeg(suffer,PCODE);
		AIP1629_WriteBlock(CMD_AIP1629_STARTADDR(0),16,segConvered); 
		runOnce = false;
		sudukuTimeBegin = millis();
		showLed(10,0);
		Serial.print("Begin");
		Serial.println(sudukuTimeBegin);
	}
	unsigned char row1 = KeyBuf[0];
	unsigned char row2 = KeyBuf[1];
	unsigned char row3 = KeyBuf[2];
	unsigned char row4 = KeyBuf[3];

	for(int i=0; i<16; i++)
	{
		if(counter16Bit[i]>=5) counter16Bit[i]=1;
	}

	AIP1629_ReadKey(4,KeyBuf); //读按键
	///左手边第一整列判断：
	if(lastKeyBuf0 == 0 && KeyBuf[0] ==  8 )
	{
		counterForKey=0;
		if(suffer[0]!=0)
		{
			int x;
			switch (suffer[0]) {
				case 1:
				x=0;
				break;
				case 2:
				x=1;
				break;
				case 3:
				x=2;
				break;
				case 4:
				x=3;
				break;
			}	
			AIP1629_WriteByte(0xc0+0,PCODE[x]);
		}
		else
		{
			AIP1629_WriteByte(0xc0+0,CODE[counter16Bit[0]]);
			counter16Bit[0]++;	
		}
	}//mouse 40
	if(lastKeyBuf0 == 0 && KeyBuf[0] ==  4 )
	{
		counterForKey=0;
		if(suffer[4]!=0)
		{
			int x;
			switch (suffer[4]) 
			{
				case 1:
				x=0;
				break;
				case 2:
				x=1;
				break;
				case 3:
				x=2;
				break;
				case 4:
				x=3;
				break;
			}	
			AIP1629_WriteByte(0xc0+2,PCODE[x]);
		}
		else
		{
			AIP1629_WriteByte(0xc0+2,CODE[counter16Bit[4]]);
			counter16Bit[4]++;	
		}
	}
	if(lastKeyBuf0 == 0 && KeyBuf[0] == 128)
	{
		counterForKey=0;
		if(suffer[8]!=0)
		{
			int x;
			switch (suffer[8]) 
			{
				case 1:
				x=0;
				break;
				case 2:
				x=1;
				break;
				case 3:
				x=2;
				break;
				case 4:
				x=3;
				break;
			}	
			AIP1629_WriteByte(0xc0+4,PCODE[x]);
		}
		else
		{
			AIP1629_WriteByte(0xc0+4,CODE[counter16Bit[8]]);
			counter16Bit[8]++;	
		}
	}
	if(lastKeyBuf0 == 0 && KeyBuf[0] ==  64)
	{
		counterForKey=0;
		if(suffer[12]!=0)
		{
			int x;
			switch (suffer[12]) 
			{
				case 1:
				x=0;
				break;
				case 2:
				x=1;
				break;
				case 3:
				x=2;
				break;
				case 4:
				x=3;
				break;
			}	
			AIP1629_WriteByte(0xc0+6,PCODE[x]);
		}
		else
		{
			AIP1629_WriteByte(0xc0+6,CODE[counter16Bit[12]]);
			counter16Bit[12]++;	
		}
	}
    ///右手边第二整列判断：
	if(lastKeyBuf1 == 0 && KeyBuf[1] == 8)
	{
		counterForKey=0;
		if(suffer[1]!=0)
		{
			int x;
			switch (suffer[1]) 
			{
				case 1:
				x=0;
				break;
				case 2:
				x=1;
				break;
				case 3:
				x=2;
				break;
				case 4:
				x=3;
				break;
			}	
			AIP1629_WriteByte(0xc0+0+8,PCODE[x]);
		}
		else 
		{
			AIP1629_WriteByte(0xc0+0+8,CODE[counter16Bit[1]]);
			counter16Bit[1]++;	
		}
	}
	if(lastKeyBuf1 == 0 && KeyBuf[1] == 4)
	{
		counterForKey=0;
		if(suffer[5]!=0)
		{
			int x;
			switch (suffer[5]) {
				case 1:
				x=0;
				break;
				case 2:
				x=1;
				break;
				case 3:
				x=2;
				break;
				case 4:
				x=3;
				break;
			}	
			AIP1629_WriteByte(0xc0+2+8,PCODE[x]);
		}
		else 
		{
			AIP1629_WriteByte(0xc0+2+8,CODE[counter16Bit[5]]);
			counter16Bit[5]++;	
		}
	}
	if(lastKeyBuf1 == 0 && KeyBuf[1] == 128)
	{
		counterForKey=0;
		if(suffer[9]!=0)
		{
			int x;
			switch (suffer[9]) {
				case 1:
				x=0;
				break;
				case 2:
				x=1;
				break;
				case 3:
				x=2;
				break;
				case 4:
				x=3;
				break;	
			}
			AIP1629_WriteByte(0xc0+4+8,PCODE[x]);
		}
		else 
		{
			AIP1629_WriteByte(0xc0+4+8,CODE[counter16Bit[9]]);
			counter16Bit[9]++;	
		}
	}

	if(lastKeyBuf1 == 0 && KeyBuf[1] == 64)
	{
		counterForKey=0;
		if(suffer[13]!=0)
		{
			int x;
			switch (suffer[13]) {
				case 1:
				x=0;
				break;
				case 2:
				x=1;
				break;
				case 3:
				x=2;
				break;
				case 4:
				x=3;
				break;	
			}
			AIP1629_WriteByte(0xc0+6+8,PCODE[x]);
		}
		else 
		{
			AIP1629_WriteByte(0xc0+6+8,CODE[counter16Bit[13]]);
			counter16Bit[13]++;	
		}
	}
	///右手边第三整列判断：
	if(lastKeyBuf2 == 0 && KeyBuf[2] == 8)
	{
		counterForKey=0;
		if(suffer[2]!=0)
		{
			int x;
			switch (suffer[2]) {
				case 1:
				x=0;
				break;
				case 2:
				x=1;
				break;
				case 3:
				x=2;
				break;
				case 4:
				x=3;
				break;		  	      
			}
			AIP1629_WriteByte(0xc0+0+1,PCODE[x]);
		}
		else 
		{
			AIP1629_WriteByte(0xc0+0+1,CODE[counter16Bit[2]]);
			counter16Bit[2]++;	
		}
	}
	if(lastKeyBuf2 == 0 && KeyBuf[2] == 4)
	{
		counterForKey=0;
		if(suffer[6]!=0)
		{
			int x;
			switch (suffer[6]) {
				case 1:
				x=0;
				break;
				case 2:
				x=1;
				break;
				case 3:
				x=2;
				break;
				case 4:
				x=3;
				break;		  	      
			}
			AIP1629_WriteByte(0xc0+2+1,PCODE[x]);
		}
		else 
		{
			AIP1629_WriteByte(0xc0+2+1,CODE[counter16Bit[6]]);
			counter16Bit[6]++;	
		}
	}
	if(lastKeyBuf2 == 0 && KeyBuf[2] == 128)
	{
		counterForKey=0;
		if(suffer[10]!=0)
		{
			int x;
			switch (suffer[10]) {
				case 1:
				x=0;
				break;
				case 2:
				x=1;
				break;
				case 3:
				x=2;
				break;
				case 4:
				x=3;
				break;		  	      
			}
			AIP1629_WriteByte(0xc0+4+1,PCODE[x]);
		}
		else 
		{
			AIP1629_WriteByte(0xc0+4+1,CODE[counter16Bit[10]]);
			counter16Bit[10]++;	
		}
	}
	if(lastKeyBuf2 == 0 && KeyBuf[2] == 64)
	{
		counterForKey=0;
		if(suffer[14]!=0)
		{
			int x;
			switch (suffer[14]) {
				case 1:
				x=0;
				break;
				case 2:
				x=1;
				break;
				case 3:
				x=2;
				break;
				case 4:
				x=3;
				break;		  	      
			}
			AIP1629_WriteByte(0xc0+6+1,PCODE[x]);
		}
		else 
		{
			AIP1629_WriteByte(0xc0+6+1,CODE[counter16Bit[14]]);
			counter16Bit[14]++;	
		}
	}
	///右手边第三整列判断：
	if(lastKeyBuf3 == 0 && KeyBuf[3] == 8)
	{
		counterForKey=0;
		if(suffer[3]!=0)
		{
			int x;
			switch (suffer[3]) {
				case 1:
				x=0;
				break;
				case 2:
				x=1;
				break;
				case 3:
				x=2;
				break;
				case 4:
				x=3;
				break;		  	      
			}
			AIP1629_WriteByte(0xc0+0+9,PCODE[x]);
		}
		else 
		{
			AIP1629_WriteByte(0xc0+0+9,CODE[counter16Bit[3]]);
			counter16Bit[3]++;	
		}
	}
	if(lastKeyBuf3 == 0 && KeyBuf[3] == 4)
	{
		counterForKey=0;
		if(suffer[7]!=0)
		{
			int x;
			switch (suffer[7]) {
				case 1:
				x=0;
				break;
				case 2:
				x=1;
				break;
				case 3:
				x=2;
				break;
				case 4:
				x=3;
				break;		  	      
			}
			AIP1629_WriteByte(0xc0+2+9,PCODE[x]);
		}
		else 
		{
			AIP1629_WriteByte(0xc0+2+9,CODE[counter16Bit[7]]);
			counter16Bit[7]++;	
		}
	}
	if(lastKeyBuf3 == 0 && KeyBuf[3] == 128)
	{
		counterForKey=0;
		if(suffer[11]!=0)
		{
			int x;
			switch (suffer[11]) {
				case 1:
				x=0;
				break;
				case 2:
				x=1;
				break;
				case 3:
				x=2;
				break;
				case 4:
				x=3;
				break;		  	      
			}
			AIP1629_WriteByte(0xc0+4+9,PCODE[x]);
		}
		else 
		{
			AIP1629_WriteByte(0xc0+4+9,CODE[counter16Bit[11]]);
			counter16Bit[11]++;	
		}
	}
	if(lastKeyBuf3 == 0 && KeyBuf[3] == 64)
	{
		counterForKey=0;
		if(suffer[15]!=0)
		{
			int x;
			switch (suffer[15]) {
				case 1:
				x=0;
				break;
				case 2:
				x=1;
				break;
				case 3:
				x=2;
				break;
				case 4:
				x=3;
				break;		  	      
			}
			AIP1629_WriteByte(0xc0+6+9,PCODE[x]);
		}
		else 
		{

			AIP1629_WriteByte(0xc0+6+9,CODE[counter16Bit[15]]);
			counter16Bit[15]++;	
		}
	}

	if(KeyBuf[0] == 8){counterForKey++;if(counterForKey>=timeThreshold){if(suffer[0]==0)AIP1629_WriteByte(0xC0+0,0x00);counterForKey=0;}}
	if(KeyBuf[0] == 4){counterForKey++;if(counterForKey>=timeThreshold){if(suffer[4]==0)AIP1629_WriteByte(0xC0+2,0x00);counterForKey=0;}}
	if(KeyBuf[0] == 128){counterForKey++;if(counterForKey>=timeThreshold){if(suffer[8]==0)AIP1629_WriteByte(0xC0+4,0x00);counterForKey=0;}}
	if(KeyBuf[0] == 64){counterForKey++;if(counterForKey>=timeThreshold){if(suffer[12]==0)AIP1629_WriteByte(0xC0+6,0x00);counterForKey=0;}}
	if(KeyBuf[1] == 8){counterForKey++;if(counterForKey>=timeThreshold){if(suffer[1]==0)AIP1629_WriteByte(0xC0+8,0x00);counterForKey=0;}}
	if(KeyBuf[1] == 4){counterForKey++;if(counterForKey>=timeThreshold){if(suffer[5]==0)AIP1629_WriteByte(0xC0+10,0x00);counterForKey=0;}}
	if(KeyBuf[1] == 128){counterForKey++;if(counterForKey>=timeThreshold){if(suffer[9]==0)AIP1629_WriteByte(0xC0+12,0x00);counterForKey=0;}}
	if(KeyBuf[1] == 64){counterForKey++;if(counterForKey>=timeThreshold){if(suffer[13]==0)AIP1629_WriteByte(0xC0+14,0x00);counterForKey=0;}}
	if(KeyBuf[2] == 8){counterForKey++;if(counterForKey>=timeThreshold){if(suffer[2]==0)AIP1629_WriteByte(0xC0+1,0x00);counterForKey=0;}}
	if(KeyBuf[2] == 4){counterForKey++;if(counterForKey>=timeThreshold){if(suffer[6]==0)AIP1629_WriteByte(0xC0+3,0x00);counterForKey=0;}}
	if(KeyBuf[2] == 128){counterForKey++;if(counterForKey>=timeThreshold){if(suffer[10]==0)AIP1629_WriteByte(0xC0+5,0x00);counterForKey=0;}}
	if(KeyBuf[2] == 64){counterForKey++;if(counterForKey>=timeThreshold){if(suffer[14]==0)AIP1629_WriteByte(0xC0+7,0x00);counterForKey=0;}}
	if(KeyBuf[3] == 8){counterForKey++;if(counterForKey>=timeThreshold){if(suffer[3]==0)AIP1629_WriteByte(0xC0+9,0x00);counterForKey=0;}}
	if(KeyBuf[3] == 4){counterForKey++;if(counterForKey>=timeThreshold){if(suffer[7]==0)AIP1629_WriteByte(0xC0+11,0x00);counterForKey=0;}}
	if(KeyBuf[3] == 128){counterForKey++;if(counterForKey>=timeThreshold){if(suffer[11]==0)AIP1629_WriteByte(0xC0+13,0x00);counterForKey=0;}}
	if(KeyBuf[3] == 64){counterForKey++;if(counterForKey>=timeThreshold){if(suffer[15]==0)AIP1629_WriteByte(0xC0+15,0x00);counterForKey=0;}}

	lastKeyBuf0 = KeyBuf[0];
	lastKeyBuf1 = KeyBuf[1];
	lastKeyBuf2 = KeyBuf[2];
	lastKeyBuf3 = KeyBuf[3];
}

void writeBit()
{

}
void play(unsigned char addr)
{
	digitalWrite(AUDIO,LOW);// 	sda=0;
	delay(3);  // wait(300); /*>2ms*/
	for (int i = 0; i < 8; i++)
		{
			digitalWrite(AUDIO, HIGH); 
			if(addr&1){
			     delayMicroseconds(2800);
			     digitalWrite(AUDIO, LOW);
			     delayMicroseconds(1200);
			 } 
			 else 
			 {
				 delayMicroseconds(1200);
				 digitalWrite(AUDIO, LOW);
				 delayMicroseconds(2800);
			 }
			 addr>>=1;	
		}	
	digitalWrite(AUDIO, HIGH);
}
void changeBrightness()
{
	static unsigned char add = 0;
	AIP1629_ReadKey(4,KeyBuf); //读按键
	if(KeyBuf[0]==72&&lastKey1 == 8){
		add--;
	    AIP1629_SendCmd(0x88+add); 
	    //Serial.println(add);
	}
	if(KeyBuf[0]==136&&lastKey1 == 8){
		add++;
	    AIP1629_SendCmd(0x88+add); 
	    //Serial.println(add);
	}
	if(add>=7){
	    add = 6;
	}
	if(add<=0){
	    add = 1;
	}
}
void changeVolume()
{
	static unsigned char add = 0;
	AIP1629_ReadKey(4,KeyBuf); //读按键
	if(KeyBuf[3]==72&&lastKey2 == 8){
		add--;
	    play(0xe0+add); 
	    delay(100);
	    play(0x04);
	    //Serial.println(add);
	}
	if(KeyBuf[3]==136&&lastKey2 == 8){
		add++;
	    play(0xe0+add); 
	    delay(100);
	    play(0x04);
	    //Serial.println(add);
	}
	if(add>=7){
	    add = 6;
	}
	if(add<=0){
	    add = 1;
	}

}
void power()
{
	
	// suduNum = random(0, 119);
    //runOnce = true; 
    autopower = 0;
    offAll = 1;
	Serial.println("Weak UP");
	 // digitalWrite(OFFLED, 1);
	// detachInterrupt(digitalPinToInterrupt(MULTIFUNCTION));
}
void systemOff()
{
	offAll = 0;
    for(int i=6; i<14; i++)
	{
	    digitalWrite(i,0);
	}
	digitalWrite(A0, 0);
	digitalWrite(LED2, 0);
	AIP1629_WriteBlock(CMD_AIP1629_STARTADDR(0),16,DspBuf);
	attachInterrupt(digitalPinToInterrupt(MULTIFUNCTION), power, CHANGE);
	Config(false);
	energy.PowerDown();
	
}
void autoPowerOff()
{
  if(KeyBuf[0]==0&&KeyBuf[1] == 0&&KeyBuf[2]==0&&KeyBuf[3]==0)
  {
      autopower++;
  }
  else
  	autopower = 0;
  	// Serial.println(autopower);
  if(autopower>=36000)// FOR ABOUT 70 SECONDS
  {
      systemOff();
  }
}
boolean isGen = true;
int mouse  = 1;
int caculate = 0;
int mouseHere = 0,lastMouseHrer = 0;
boolean ifAutoGone = false,lost = false,live = false;
unsigned long timeBeatMouseBegin[16]={};
unsigned long timeBeatMouseStop[16]={};
unsigned long lastTime_mouse_gone = 0,lastTime_mouse_gone_1,lastTime_mouse_gone_2,lastTime_mouse_gone_3;
unsigned char killState[16]= {2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2};
unsigned char keystatus[16]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
unsigned char theNumberMouse = 18,theLastNumberMouse;
int galoble_score = 3,galoble_frequen = 1000,galoble_gone_us = 700;
void genarateMouse(boolean begin,int frequency_us,int mouse_gone_us)
{
	if((millis() - lastTime_mouse>=frequency_us) && begin==true)
	{		
		while(mouseHere == lastMouseHrer)
		{
			mouseHere = random(0, 16);    
			
		}
		Serial.println(mouseHere);
	    lastTime_mouse =millis();
	    AIP1629_WriteByte(CMD_AIP1629_STARTADDR(mouseHere),HDIGN);
	    killState[mouseHere] = 0; 
	    lastTime_mouse_gone = lastTime_mouse;  
	    lastMouseHrer =mouseHere;//记录上一次的随机值；
	}  
	if(killState[mouseHere]==0 && (millis()-lastTime_mouse_gone>=mouse_gone_us) && begin==true)//没打到地鼠，就会自动消失；
	{
	    AIP1629_WriteByte(CMD_AIP1629_STARTADDR(mouseHere),BACKGROUND_SIGN); //这里来放置音效 ”没打到“
	    delay(100);//(10000);
	    AIP1629_WriteByte(CMD_AIP1629_STARTADDR(mouseHere),HDIGN); //这里来放置音效 ”没打到“
	    delay (100);
	    AIP1629_WriteByte(CMD_AIP1629_STARTADDR(mouseHere),BACKGROUND_SIGN); //这里来放置音效 ”没打到“
	    //减去一盏灯；
	    //播放声音 没打到
	    lastTime_mouse_gone = millis();
	    showLed(10,0);
	    galoble_score--;
	    showLed(galoble_score,1);
	    lost = true;
	}	
}

void killMouse(unsigned char number)
{	
	static unsigned int score = 1;;

	if(theNumberMouse!=theLastNumberMouse && live== true&& number==mouseHere)
	{
		AIP1629_WriteByte(CMD_AIP1629_STARTADDR(number),BACKGROUND_SIGN);
		score++;
		if(score%3==0)
		{
			galoble_score++;
		}
		showLed(galoble_score,1); 
		lost = false;
	}
}
void beatMouseWin()
{
    if(galoble_score==10 && lost == false)
	{
		
		//播放:下一关卡声音
		//改变参数让游戏更难
		//重置显示分数（显示）
		galoble_frequen = galoble_frequen-50;
		galoble_gone_us = galoble_gone_us-50;

		isGen = true;
		live = true;
		mouse = 1;

		showLed(galoble_score,1);
		delay(500);
		showLed(galoble_score,0);
		delay(500);
		showLed(galoble_score,1);
		delay(500);
		showLed(galoble_score,0);

		Serial.println("Win-------------------------------");
		galoble_score = 3;
	}
	if(galoble_score <= 0 && lost== true )
	{
	    isGen = false;
	    live = false;
	    showLed(10,0);
	    AIP1629_WriteBlock(CMD_AIP1629_STARTADDR(0),16,mosueDeadArray);
	    Serial.println("dead-------------------------------");
	}
}
void beatMouse()
{	
	if(mouse)
	{

		 															   //            ----
	    AIP1629_WriteBlock(CMD_AIP1629_STARTADDR(0),16,backGroud);//绘制背景图标----
	    mouse = 0;													   //            ----
	    //加入倒数计时 为用户准备时间
	    //galoble_score = 3;
	    for(int i=1; i<=3; i++){
	       showLed(i,1); 
	       delay(200);
	    }
	    delay(1000);
	}
	
    genarateMouse(isGen,galoble_frequen,galoble_gone_us);//随机生成地鼠
    AIP1629_ReadKey(4,KeyBuf); //读按键

	if((lastKeyBuf0 == 0 && KeyBuf[0] ==  8 )|| (lastKeyBuf0 == 8 && KeyBuf[0] == 0) ){killState[0]=1;theNumberMouse = 0;if(mouseHere == 0){caculate=0;}Serial.println("0");}//mouse 40
	if((lastKeyBuf0 == 0 && KeyBuf[0] == 4 )|| (lastKeyBuf0 == 4 && KeyBuf[0] == 0) ){killState[2]=1;theNumberMouse = 2;if(mouseHere == 2){caculate=0;}Serial.println("2");}
	if((lastKeyBuf0 == 0 && KeyBuf[0] == 128 )|| (lastKeyBuf0 == 128 && KeyBuf[0] == 0)){killState[4]=1;theNumberMouse = 4;if(mouseHere == 4){caculate=0;}Serial.println("4");}
	if((lastKeyBuf0 == 0 && KeyBuf[0] == 64 )|| (lastKeyBuf0 == 64 && KeyBuf[0] == 0)){killState[6]=1;theNumberMouse = 6;if(mouseHere == 6){caculate=0;}Serial.println("6");}

	if((lastKeyBuf1 == 0 && KeyBuf[1] == 8 )|| (lastKeyBuf1 == 8 && KeyBuf[1] == 0)){killState[8]=1;theNumberMouse = 8;if(mouseHere == 8){caculate=0;}Serial.println("8");}
	if((lastKeyBuf1 == 0 && KeyBuf[1] == 4 )|| (lastKeyBuf1 == 4 && KeyBuf[1] == 0)){killState[10]=1;theNumberMouse = 10;if(mouseHere == 10){caculate=0;}Serial.println("10");}
	if((lastKeyBuf1 == 0 && KeyBuf[1] == 128 )|| (lastKeyBuf1 == 128 && KeyBuf[1] == 0)){killState[12]=1;theNumberMouse = 12;if(mouseHere == 12){caculate=0;}Serial.println("12");}
	if((lastKeyBuf1 == 0 && KeyBuf[1] == 64 )|| (lastKeyBuf1 == 64 && KeyBuf[1] == 0)){killState[14]=1;theNumberMouse = 14;if(mouseHere == 14){caculate=0;}Serial.println("14");}

	if((lastKeyBuf2 == 0 && KeyBuf[2] == 8 )|| (lastKeyBuf2 == 8 && KeyBuf[2] == 0)){killState[1]=1;theNumberMouse = 1;if(mouseHere == 1){caculate=0;}Serial.println("1");}
	if((lastKeyBuf2 == 0 && KeyBuf[2] == 4 )|| (lastKeyBuf2 == 4 && KeyBuf[2] == 0)){killState[3]=1;theNumberMouse = 3;if(mouseHere == 3){caculate=0;}Serial.println("3");}
	if((lastKeyBuf2 == 0 && KeyBuf[2] == 128 )|| (lastKeyBuf2 == 128 && KeyBuf[2] == 0)){killState[5]=1;theNumberMouse = 5;if(mouseHere == 5){caculate=0;}Serial.println("5");}
	if((lastKeyBuf2 == 0 && KeyBuf[2] == 64 )|| (lastKeyBuf2 == 64 && KeyBuf[2] == 0)){killState[7]=1;theNumberMouse = 7;if(mouseHere == 7){caculate=0;}Serial.println("7");}

	if((lastKeyBuf3 == 0 && KeyBuf[3] == 8 )|| (lastKeyBuf3 == 8 && KeyBuf[3] == 0)){killState[9]=1;theNumberMouse = 9;if(mouseHere == 9){caculate=0;}Serial.println("9");}
	if((lastKeyBuf3 == 0 && KeyBuf[3] == 4 )|| (lastKeyBuf3 == 4 && KeyBuf[3] == 0)){killState[11]=1;theNumberMouse = 11;if(mouseHere == 11){caculate=0;}Serial.println("11");}
	if((lastKeyBuf3 == 0 && KeyBuf[3] == 128 )|| (lastKeyBuf3 == 128 && KeyBuf[3] == 0)){killState[13]=1;theNumberMouse = 13;if(mouseHere == 13){caculate=0;}Serial.println("13");}
	if((lastKeyBuf3 == 0 && KeyBuf[3] == 64 )|| (lastKeyBuf3 == 64 && KeyBuf[3] == 0)){killState[15]=1;theNumberMouse = 15;if(mouseHere == 15){caculate=0;}Serial.println("15");}
	
   // AIP1629_ReadKey(4,KeyBuf); //读按键
   // Serial.println(theNumberMouse);
	killMouse(theNumberMouse);
	theLastNumberMouse = theNumberMouse;
 
	lastKeyBuf0 = KeyBuf[0];
	lastKeyBuf1 = KeyBuf[1];
	lastKeyBuf2 = KeyBuf[2];
	lastKeyBuf3 = KeyBuf[3];
	
}


